from selenium import webdriver
from time import sleep
from PIL import Image
# from .api import main
from selenium.common.exceptions import NoSuchElementException
from .login import login_test
from .verification import main
from selenium.webdriver.chrome.options import Options


def open_browser(browser_name, url):
    driver = None
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--window-size=1920,1080')
    if browser_name == "chrome":
        driver = webdriver.Chrome(chrome_options=chrome_options)
    if browser_name == "ff":
        driver = webdriver.Firefox()
    if browser_name == "ie":
        driver = webdriver.Ie()
    driver.maximize_window()
    driver.implicitly_wait(10)
    driver.get(url)
    return driver


class TestKeys(object):

    def __init__(self, browser_name, url, username, password, new_password):
        self.driver = open_browser(browser_name, url)
        self.username = username
        self.password = password
        self.new_password = new_password

    def webui1(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 角色管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[4]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 筛选条件
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["role_name"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/footer/div/div/slot/span').text
            print(error_text)
            if error_text == "":
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/main/div/div[3]/div/span').text
                print(error_text)
            # 重置
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[2]').click()
            sleep(3)
            returns_list.append(error_text)
            driver.refresh()
            sleep(3)
            continue
        print(returns_list)
        return returns_list

    def webui2(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 角色管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[4]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 新增角色
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/header/div[2]/button[1]').click()
            sleep(1)
            if dicts["role_name"] is not None:
                # 角色名称
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/div/div[1]/input').send_keys(dicts["role_name"])
                sleep(1)
            if dicts["role_coding"] is not None:
                # 角色编码
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/div/div[1]/input').send_keys(dicts["role_coding"])
                sleep(1)
            if dicts["role_property"] is not None:
                # 角色性质-单位级
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div/div/div/div/span/span').click()
                sleep(2)
                if dicts["role_name"] is None:
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[2]/span').click()
                    sleep(1)
                else:
                    driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[1]/ul/li[2]/span').click()
                    sleep(1)
            if dicts["department"] is not None:
                # 所属部门-ROOT
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/div/div/div/div[1]/span/span').click()
                sleep(2)
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/div/div/div/div[2]/div[1]/div[1]/ul/div/div[1]/div[1]/span[2]').click()
                sleep(1)
            # 描述
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div/div/div/textarea').send_keys("描述测试01")
            sleep(1)
            # 保存
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/div/button[2]').click()
            sleep(2)
            if dicts["role_name"] == dicts["role_name_2"]:
                alert_text = driver.find_element_by_xpath('/html/body/div[4]/p').text
                print(alert_text)
                returns_list.append(alert_text)
                driver.refresh()
                sleep(3)
                continue
            if dicts["role_name"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                sleep(3)
                continue
            if dicts["role_coding"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                sleep(3)
                continue
            if dicts["role_property"] is None:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                sleep(3)
                continue
            if dicts["department"] is None:
                alert_text = driver.find_element_by_xpath('/html/body/div[4]/p').text
                print(alert_text)
                returns_list.append(alert_text)
                driver.refresh()
                sleep(3)
                continue
            if len(dicts["role_name"]) > 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                sleep(3)
                continue
            if len(dicts["role_coding"]) > 32:
                error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/div/div[2]').text
                print(error_text)
                returns_list.append(error_text)
                driver.refresh()
                sleep(3)
                continue

            returns_list.append("新增成功")
            sleep(2)
            # 角色名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["role_name"])
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)

        print(returns_list)
        return returns_list

    def webui3(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 角色管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[4]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 角色名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["role_name"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)
            # 编辑
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/main/div/div[3]/table/tbody/tr/td[8]/div/button[1]').click()
            sleep(1)
            if dicts["role_name_2"] is not None:
                # 角色名称
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/div/div[1]/input').clear()
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[1]/div/div/div/div[1]/input').send_keys(
                    dicts["role_name_2"])
                sleep(1)
            if dicts["role_coding"] is not None:
                # 角色编码
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/div/div[1]/input').clear()
                driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[2]/div/div/div/div[1]/input').send_keys(
                    dicts["role_coding"])
                sleep(1)
            if dicts["role_property"] is not None:
                # 角色性质-单位级
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[3]/div/div/div/div/div/span/span').click()
                sleep(2)
                if dicts["role_name_2"] is None:
                    driver.find_element_by_xpath('/html/body/div[6]/div[1]/div[1]/ul/li[3]/span').click()
                    sleep(1)
                else:
                    driver.find_element_by_xpath('/html/body/div[7]/div[1]/div[1]/ul/li[3]').click()
                    sleep(1)
            if dicts["department"] is not None:
                # 所属部门-ROOT
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/div/div/div/div[1]/input').click()
                sleep(2)
                driver.find_element_by_xpath(
                    '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[4]/div/div/div/div/div/div[2]/div[1]/div[1]/ul/div/div[2]').click()
                sleep(1)
            # 描述
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/form/div[5]/div/div/div/div/textarea').send_keys(
                "描述测试PUT")
            sleep(1)
            # 保存
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[3]/div/div[2]/div/div/button[2]').click()
            sleep(2)
            # 重置
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[2]').click()
            sleep(2)
            # 角色名称
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(
                dicts["role_name_2"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/footer/div/div/slot/span').text
            print(error_text)
            if error_text == "共 1 条":
                returns_list.append("编辑成功")
            else:
                returns_list.append("编辑失败")
            # 重置
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[2]').click()
            sleep(1)
            continue
        print(returns_list)
        return returns_list

    def webui4(self, list_dicts):
        # 登录
        driver = login_test(self.username, self.driver, self.new_password)
        # 系统管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]').click()
        # 角色管理
        driver.find_element_by_xpath('//*[@id="app"]/section/section/aside/div/ul/div/li[6]/ul/div/li[4]').click()
        sleep(1)
        returns_list = []
        for dicts in list_dicts:
            # 角色名称
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(dicts["role_name_2"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)
            # 删除
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/main/div/div[3]/table/tbody/tr/td[8]/div/button[2]').click()
            sleep(1)
            # 确定
            driver.find_element_by_xpath('/html/body/div[6]/div/div[3]/button[2]').click()
            sleep(1)
            # 重置
            driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[2]').click()
            sleep(2)
            # 角色名称
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[1]/div/div/div/input').send_keys(
                dicts["role_name_2"])
            sleep(1)
            # 查询
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[1]').click()
            sleep(2)
            error_text = driver.find_element_by_xpath('//*[@id="app"]/section/section/main/section/div/div[2]/div[1]/div/main/div/div[3]/div/span').text
            print(error_text)
            if error_text == "暂无数据":
                returns_list.append("删除成功")
            else:
                returns_list.append("删除失败")
            # 重置
            driver.find_element_by_xpath(
                '//*[@id="app"]/section/section/main/section/div/div[1]/main/div/main/form/div/div[2]/button[2]').click()
            sleep(1)
            continue
        print(returns_list)
        return returns_list

    def quit(self):
        """
        浏览器退出
        :return:
        """
        self.driver.quit()